package com.kcnet.qk.controller.hdyh;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;
import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/5/3 0003.
 */

public class HdFriendTimeLineZans extends BasePeopleController {

    private static String TAG = "好友时间线点赞";

    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());

    private int maxAddFriendCount = 3; //最大添加10人
    private int currentAddFriendCount = 0; //当前任务添加人数

    private static HdFriendTimeLineZans mRongCloudInstance;


    private boolean isJoinFriendsList = false;
    private boolean isJoinFriendsDetail = false;


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static HdFriendTimeLineZans getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (HdFriendTimeLineZans.class) {
                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new HdFriendTimeLineZans();
                }
            }
        }

        return mRongCloudInstance;
    }




    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();



        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击到对应模块");
            // sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TAB_JHYQQ));
            sleepTimedDefault();
            searchClick();
        } else if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.DWZ")) {
            NLogger.d(TAG, "2、点击 全部好友");
            searchTypeClick(FBStringUtil.getString(StringConst.ADDFRIEND_ALL_FRIEND));
            initFbMain = true;
            isJoinFriendsList = true;
        }else if (isJoinFriendsList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            NLogger.d(TAG, "3、进入好友列表 开始自动点击好友列表");
            beginAddFriendList();
        }
        else if (isJoinFriendsDetail && mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView")) {
            NLogger.d(TAG, "4、进入好友详情界面 开始点赞");
            beginAddFriendAdopt();
        }
        else if (isJoinFriendsList && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "5、回到好友列表 继续点击好友进行添加");
            beginAddFriendList();
        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.2Bh")) { //弹出对话框自动点击确认
            if (findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEDING))) {
            } else {
                findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEREN));
            }
        }

    }

    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendList() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }

                for (int i = 0; i < scrllorNodeInfo.getChildCount(); i++) {
                    if (currentAddCount == maxAddFriendCount) {
                        currentAddCount = 0;
                        mRecordMap.clear();
                        stopTask();
                        //停止任务
                        break;
                    }
                    AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(i);
                    Log.d("ND", "nodeInfo size" + nodeInfo.getChildCount());
                    if (nodeInfo.getChildCount() > 2) {
                        if (!mRecordMap.containsKey(nodeInfo.getContentDescription().toString())) {

                            sleepTimedDefault();
                            isJoinFriendsDetail = NodeUtil.performClick(nodeInfo);
                            if (isJoinFriendsDetail) {
                                mRecordMap.put(nodeInfo.getContentDescription().toString(), true);
                                currentAddCount++;
                                isJoinFriendsList = false;
                                break;
                            }

                            NLogger.d(TAG, "点击了好友列表 " + isJoinFriendsDetail);

                            break;
                        }

                    }

                    if (i == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }

                }


            }
        }
    }

    public void friendHomeGotoBack() {
        AccessibilityNodeInfo root = getRoot();
        List<AccessibilityNodeInfo> list = root.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.BACK));
        if (!list.isEmpty()) {
            NodeUtil.performClick(list.get(0));
            NLogger.d(TAG, "点击返回 回到好友主页 并且再次回退");
        }
    }
    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendAdopt(){
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if(scrllorNodeInfos.size() > 0){
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if(scrllorNodeInfo == null){
                    return;
                }

                for(int q=0;q<scrllorNodeInfo.getChildCount();q++){

                    if(currentAddFriendCount  == maxAddCount){
                        currentAddFriendCount = 0;
                        isJoinFriendsDetail = false;
                        isJoinFriendsList = true;
                        friendHomeGotoBack();
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    List<AccessibilityNodeInfo> list = as.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.HDYH_ZAN));
                    if(!list.isEmpty()){
                        AccessibilityNodeInfo zanNode = list.get(0);

                        if(!zanNode.getContentDescription().toString().equals(FBStringUtil.getString(StringConst.HDYH_ZAN_YAX))){
                            sleepTimedDefault();
                            currentAddCount++;
                            NLogger.d(TAG,"点击了点赞按钮"+   NodeUtil.performClick(zanNode));
                            break;
                        }

                    }


                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime,int maxAddFriendCount){
       // stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        initFbMain = false;
         this.maxAddFriendCount = maxAddFriendCount; //最大添加10人
          this.currentAddFriendCount = 0; //当前任务添加人数

       isJoinFriendsList  =false;
        isJoinFriendsDetail =false;
    }

}
